package com.skymxc.demo.explorer.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.text.TextUtils;

import com.skymxc.demo.explorer.R;
import com.skymxc.demo.explorer.SettingActivity;

/**
 * Created by sky-mxc
 */
public class SettingFragment extends PreferenceFragment implements Preference.OnPreferenceChangeListener{

    private ListPreference showMode;
    private ListPreference theme;
    private CheckBoxPreference showHiddenFile;
    private CheckBoxPreference showImageThumbnail;
    private CheckBoxPreference rememberPath;
    private EditTextPreference compressPath;
    private EditTextPreference uncompressPath;

    private String[] modeSummary;
    private String[] hiddenSummary;
    private String[] thumbSummary;
    private String[] rememberSummary;
    private String[] themeEntries;
    private String themeSummary;
    private String compressSummary;
    private String uncompressSummary;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preference_setting);
        showMode = (ListPreference) findPreference(getString(R.string.show_mode_key));
        theme = (ListPreference) findPreference(getString(R.string.theme_key));
        showHiddenFile = (CheckBoxPreference) findPreference(getString(R.string.show_hidden_file_key));
        showImageThumbnail = (CheckBoxPreference) findPreference(getString(R.string.show_image_thumbnail_key));
        rememberPath = (CheckBoxPreference) findPreference(getString(R.string.remember_path_key));
        compressPath = (EditTextPreference) findPreference(getString(R.string.compress_path_key));
        uncompressPath = (EditTextPreference) findPreference(getString(R.string.uncompress_path_key));
        initSummary();
        initPreferenceListener();
    }

    /**
     * 初始化 监听
     */
    private void initPreferenceListener() {
        showMode.setOnPreferenceChangeListener(this);
        theme.setOnPreferenceChangeListener(this);
        showHiddenFile.setOnPreferenceChangeListener(this);
        showImageThumbnail.setOnPreferenceChangeListener(this);
        rememberPath.setOnPreferenceChangeListener(this);
        compressPath.setOnPreferenceChangeListener(this);
        uncompressPath.setOnPreferenceChangeListener(this);
    }

    /**
     * 初始化Summary
     */
    private void initSummary() {
        //将Summary 对应的字符串加载
        modeSummary = getResources().getStringArray(R.array.show_mode_summary);
        hiddenSummary = getResources().getStringArray(R.array.show_hidden_summary);
        thumbSummary = getResources().getStringArray(R.array.show_image_thumbnail_summary);
        rememberSummary= getResources().getStringArray(R.array.remember_path_summary);
        themeEntries = getResources().getStringArray(R.array.theme);
        themeSummary = getResources().getString(R.string.theme_summary);
        compressSummary = getResources().getString(R.string.compress_path_summary);
        uncompressSummary = getResources().getString(R.string.uncompress_path_summary);

        //设置summary
        setShowModeSummary(showMode.getValue());
        setThemeSummary(theme.getValue());
        setShowHiddenFileSummary(showHiddenFile.isChecked());
        setShowImageThumbnailSummary(showImageThumbnail.isChecked());
        setRememberPathSummary(rememberPath.isChecked());
        String cp = Environment.getExternalStorageDirectory().getPath()+"/explorer/compress";
        setCompressPathSummary(cp);
        compressPath.setDefaultValue(cp);


        String up = Environment.getExternalStorageDirectory().getPath()+"/explorer/uncompress";
        setUncompressPathSummary(up);
        uncompressPath.setDefaultValue(up);

    }

    /**
     * 设置 rememberPath Summary
     * @param path
     */
    private void setUncompressPathSummary(String path){
        uncompressPath.setText(path);
        uncompressPath.setSummary(uncompressSummary+" "+path);
    }

    /**
     * 设置 rememberPath Summary
     * @param path
     */
    private void setCompressPathSummary(String path){
        compressPath.setText(path);
        compressPath.setSummary(compressSummary+" "+path);
    }

    /**
     * 设置 Remember Summary
     * @param checked
     */
    private void setRememberPathSummary(boolean checked){
        if (checked){
            rememberPath.setSummary(rememberSummary[0]);
        }else{
            rememberPath.setSummary(rememberSummary[1]);
        }
    }


    /**
     * 设置 ShowImageThumbnail Summary
     * @param checked
     */
    private void setShowImageThumbnailSummary(boolean checked){
        if (checked){
            showImageThumbnail.setSummary(thumbSummary[0]);
        }else{
            showImageThumbnail.setSummary(thumbSummary[1]);
        }
    }

    /**
     * 设置 ShowHiddenFile Summary
     * @param checked
     */
    private void setShowHiddenFileSummary(boolean checked){
        if (checked){
            showHiddenFile.setSummary(hiddenSummary[0]);
        }else{
            showHiddenFile.setSummary(hiddenSummary[1]);
        }
    }

    /**
     * 设置 主题 Summary
     * @param value
     */
    private void setThemeSummary(String value){
        int v = 0;
        if (!TextUtils.isEmpty(value)){
           v =Integer.parseInt(value);
        }
        theme.setSummary(String.format(themeSummary, themeEntries[v]));
    }

    /**
     * 设置显示模式的Summary
     * @param value
     */
    private void setShowModeSummary(String  value){
        int v = 0;
        if (!TextUtils.isEmpty(value)){
            v =Integer.parseInt(value);
        }
       showMode.setSummary(modeSummary[v]);
    }

    /**
     * 改变事件
     * @param preference
     * @param newValue 新的值
     * @return
     */
    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {

        if (preference.getKey().equals(getString(R.string.show_mode_key))){
                setShowModeSummary(newValue.toString());
            getActivity().setResult(Activity.RESULT_OK);
        }else  if (preference.getKey().equals(getString(R.string.show_hidden_file_key))){
                setShowHiddenFileSummary(Boolean.parseBoolean(newValue.toString()));
            getActivity().setResult(Activity.RESULT_OK);
        }else if (preference.getKey().equals(getString(R.string.show_image_thumbnail_key))){
                setShowImageThumbnailSummary((Boolean) newValue);
            getActivity().setResult(Activity.RESULT_OK);
        }else if (preference.getKey().equals(getString(R.string.theme_key))){
                setThemeSummary(newValue.toString());
            ( (SettingActivity)getActivity()).changeTheme(Integer.parseInt(newValue.toString()));
        }else if (preference.getKey().equals(getString(R.string.compress_path_key))){
            setCompressPathSummary(newValue.toString());
        }else if (preference.getKey().equals(getString(R.string.uncompress_path_key))){
            setUncompressPathSummary(newValue.toString());
        }else if (preference.getKey().equals(getString(R.string.remember_path_key))){
            setRememberPathSummary((Boolean) newValue);
        }

            return true;
    }
}
